﻿#include <opencv2\opencv.hpp>  
#include <iostream>  
#include <fstream>
#include <string>  
#include<math.h>
using namespace cv;  
using namespace std;
void main()    
{   
    ifstream in;
    in.open("g.txt");
     int colors[256]={0};
     IplImage *src1,*src2,*src3,*src4,*src5,*src6;     
	src1 = cvCreateImage(cvSize(15,12), IPL_DEPTH_8U,3);
	src2 = cvCreateImage(cvSize(15,12), IPL_DEPTH_8U,3);
	src3 = cvCreateImage(cvSize(15,12), IPL_DEPTH_8U,3);
	src4 = cvCreateImage(cvSize(15,12), IPL_DEPTH_8U,3);
	src5 = cvCreateImage(cvSize(15,12), IPL_DEPTH_8U,3);
	src6 = cvCreateImage(cvSize(15,12), IPL_DEPTH_8U,3);
	int width=src1->width;
	int height=src1->height;
	int widthstep=src1->widthStep;
	uchar* a1=(uchar*)src1->imageData;
	uchar* a2=(uchar*)src2->imageData;
	uchar* a3=(uchar*)src3->imageData;
	uchar* a4=(uchar*)src4->imageData;
	uchar* a5=(uchar*)src5->imageData;
	uchar* a6=(uchar*)src6->imageData;
	double data1[12][15],data2[12][15],data3[12][15];
	for(int i=0;i<height;i++)
	{
	    for(int j=0;j<width;j++)
	    {
		   in>>data1[i][j];
		   in>>data2[i][j];
		   in>>data3[i][j];
	    }
	}
	int i2;
	int j2;
	int i3;
	int j3;
	for(int i=0;i<height;i++)
	{
	    for(int j=0;j<width;j++)
	    {
		  a1[ i*widthstep + j*src1->nChannels + 0]=(int)((data1[i][j])*255.0);
		  a1[ i*widthstep + j*src1->nChannels + 1]=(int)((data1[i][j])*255.0);
		  a1[ i*widthstep + j*src1->nChannels + 2]=(int)((data1[i][j])*255.0);

		  a2[ i*widthstep + j*src1->nChannels + 0]=(int)((data2[i][j])*255.0);
		  a2[ i*widthstep + j*src1->nChannels + 1]=(int)((data2[i][j])*255.0);
		  a2[ i*widthstep + j*src1->nChannels + 2]=(int)((data2[i][j])*255.0);

		  a3[ i*widthstep + j*src1->nChannels + 0]=(int)((data3[i][j])*255.0);
		  a3[ i*widthstep + j*src1->nChannels + 1]=(int)((data3[i][j])*255.0);
		  a3[ i*widthstep + j*src1->nChannels + 2]=(int)((data3[i][j])*255.0);

		  a4[ i*widthstep + j*src1->nChannels + 0]=(int)((data1[i][j]*data2[i][j]*data3[i][j])*255.0);
		  a4[ i*widthstep + j*src1->nChannels + 1]=(int)((data1[i][j]*data2[i][j]*data3[i][j])*255.0);
		  a4[ i*widthstep + j*src1->nChannels + 2]=(int)((data1[i][j]*data2[i][j]*data3[i][j])*255.0);

		  i2=i+2;
		  i3=i2+2;
		  j2=j+2;
		  j3=j2+2;
		  if(i3<height&&j3<width)
		  {
			 a5[ i*widthstep + j*src1->nChannels + 0]=(int)((data1[i][j]*data2[i2][j2]*data3[i3][j3])*255.0);
			 a5[ i*widthstep + j*src1->nChannels + 1]=(int)((data1[i][j]*data2[i2][j2]*data3[i3][j3])*255.0);
			 a5[ i*widthstep + j*src1->nChannels + 2]=(int)((data1[i][j]*data2[i2][j2]*data3[i3][j3])*255.0);
		  }
		  else if(i2<height&&j2<width)
		  {
			 a5[ i*widthstep + j*src1->nChannels + 0]=(int)((data1[i][j]*data2[i2][j2])*255.0);
			 a5[ i*widthstep + j*src1->nChannels + 1]=(int)((data1[i][j]*data2[i2][j2])*255.0);
			 a5[ i*widthstep + j*src1->nChannels + 2]=(int)((data1[i][j]*data2[i2][j2])*255.0);
		  }
		  else
		  {
			 a5[ i*widthstep + j*src1->nChannels + 0]=(int)((data1[i][j])*255.0);
			 a5[ i*widthstep + j*src1->nChannels + 1]=(int)((data1[i][j])*255.0);
			 a5[ i*widthstep + j*src1->nChannels + 2]=(int)((data1[i][j])*255.0);
		  }

		  i2=i-2;
		  i3=i2-2;
		  j2=j-2;
		  j3=j2-2;
		  if(i3>=0&&j3>=0)
		  {
			 a6[ i*widthstep + j*src1->nChannels + 0]=(int)((data1[i][j]*data2[i2][j2]*data3[i3][j3])*255.0);
			 a6[ i*widthstep + j*src1->nChannels + 1]=(int)((data1[i][j]*data2[i2][j2]*data3[i3][j3])*255.0);
			 a6[ i*widthstep + j*src1->nChannels + 2]=(int)((data1[i][j]*data2[i2][j2]*data3[i3][j3])*255.0);
		  }
		  if(i2>=0&&j2>=0)
		  {
			 a6[ i*widthstep + j*src1->nChannels + 0]=(int)((data1[i][j]*data2[i2][j2])*255.0);
			 a6[ i*widthstep + j*src1->nChannels + 1]=(int)((data1[i][j]*data2[i2][j2])*255.0);
			 a6[ i*widthstep + j*src1->nChannels + 2]=(int)((data1[i][j]*data2[i2][j2])*255.0);
		  }
		  else
		  {
			 a6[ i*widthstep + j*src1->nChannels + 0]=(int)((data1[i][j])*255.0);
			 a6[ i*widthstep + j*src1->nChannels + 1]=(int)((data1[i][j])*255.0);
			 a6[ i*widthstep + j*src1->nChannels + 2]=(int)((data1[i][j])*255.0);
		  }
	    }
	}
	in.close();
	cvSaveImage("result1.jpg",src1);
	cvNamedWindow("result1",1);  
     cvShowImage("result1",src1);  
	cvSaveImage("result2.jpg",src2);
	cvNamedWindow("result2",1);  
     cvShowImage("result2",src2);
	cvSaveImage("result3.jpg",src3);
	cvNamedWindow("result3",1);  
     cvShowImage("result3",src3);  
	cvSaveImage("result4.jpg",src4);
	cvNamedWindow("result4",1);  
     cvShowImage("result4",src4);  
	cvSaveImage("result5.jpg",src5);
	cvNamedWindow("result5",1);  
     cvShowImage("result5",src5);  
	cvSaveImage("result6.jpg",src6);
	cvNamedWindow("result6",1);  
     cvShowImage("result6",src6);  
     cvWaitKey(0);    
	cvDestroyWindow("result1");
	cvDestroyWindow("result2");
	cvDestroyWindow("result3");
	cvDestroyWindow("result4");
	cvDestroyWindow("result5");
	cvDestroyWindow("result6");
     cvReleaseImage(&src1); 
	cvReleaseImage(&src2); 
	cvReleaseImage(&src3); 
	cvReleaseImage(&src4); 
	cvReleaseImage(&src5); 
	cvReleaseImage(&src6); 
}
